from zope.interface import implements
from zope.component import adapts

from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleVocabulary

from Acquisition import aq_inner
from DateTime import DateTime

from Products.Archetypes.interfaces import IObjectPostValidation
from Products.CMFCore.utils import getToolByName

from Products.Archetypes import atapi
from Products.validation import V_REQUIRED

#from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import document
#from Products.ATContentTypes.content import schemata
from Products.ATContentTypes.content.schemata import finalizeATCTSchema

from apkn.resource.interfaces import IResource
from apkn.resource.config import *

from Products.MasterSelectWidget.MasterSelectWidget import MasterSelectWidget

from AccessControl import ClassSecurityInfo
from AccessControl.SecurityManagement import newSecurityManager

from apkn.resource import ResourceMessageFactory as _

#ResourceSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
ResourceSchema = document.ATDocumentSchema.copy() + atapi.Schema((
    atapi.TextField('text',
        required=False,
        searchable=True,
        storage=atapi.AnnotationStorage(),
        validators=('isTidyHtmlWithCleanup',),
        default_output_type='text/x-html-safe',
        widget=atapi.RichWidget(label=_(u"Descriptive text"),
                                description=_(u""),
                                rows=25,
                                allow_file_upload=False),
        ),
    atapi.StringField('webaddress',
        required=True,
        searchable=True,
        storage=atapi.AnnotationStorage(),
        widget=atapi.StringWidget(label=_(u"Web address"),
                                  description=_(u"Web address of the resource."))
        ),
        
    atapi.StringField('resourcelanguage',
        required=True,
        searchable=True,
        vocabulary=RESOURCELANGUAGES,                             
        storage=atapi.AnnotationStorage(),
        widget=atapi.MultiSelectionWidget(label=_(u"Resource Language"),
                                  description=_(u"Language of the resource"))
        ),      
                
    atapi.StringField('country',
        required=True,
        searchable=True,
        vocabulary=RESOURCECOUNTRIES,
        storage=atapi.AnnotationStorage(),
        widget=MasterSelectWidget(label=_(u"Country/Continent"),
        						format='select',
                                description=_(u"Country/Continent"),
                                slave_fields=slave_fields),
        ), 
    atapi.StringField('region',
        required=False,
        searchable=True,
        storage=atapi.AnnotationStorage(),
        widget=atapi.StringWidget(label=_(u"Region"),
                                description=_(u"Region (Not editable.)"),
                                disabled='disabled'),
        ),        
    atapi.StringField('economicbloc',
        required=False,
        searchable=True,
        storage=atapi.AnnotationStorage(),
        widget=atapi.StringWidget(label=_(u"Economic Bloc"),
                                description=_(u"Economic Bloc (Not editable.)")),
        ),    
    atapi.StringField('resourcetype',
        required=True,
        searchable=True,
        vocabulary=RESOURCETYPES,                             
        storage=atapi.AnnotationStorage(),
        widget=atapi.SelectionWidget(label=_(u"Resource Type"),
                                  description=_(u"Type of resource"))
        ),       
    atapi.StringField('resourcesubject',
        required=False,
        searchable=True,
        vocabulary=RESOURCESUBJECTS,                             
        storage=atapi.AnnotationStorage(),
        widget=atapi.MultiSelectionWidget(label=_(u"Resource Subject"),
                                  description=_(u"Resource Subject (You may select more than one resource - hold the shift key and make your selections."))
        ),                                                               
    ))

ResourceSchema['title'].storage = atapi.AnnotationStorage()
ResourceSchema['title'].widget.label = _(u"Resource Title")
ResourceSchema['title'].widget.description = _(u"")

ResourceSchema['description'].storage = atapi.AnnotationStorage()
ResourceSchema['description'].widget.label = _(u"Brief description of the resource")
ResourceSchema['description'].widget.description = _(u"")

finalizeATCTSchema(ResourceSchema, folderish=False, moveDiscussion=False)

class Resource(document.ATDocument):
    """Describe a resource.
    """
    implements(IResource)
    
    portal_type = "Resource"
    _at_rename_after_creation = True
    schema = ResourceSchema
    
    title = atapi.ATFieldProperty('title')
    description = atapi.ATFieldProperty('description')
    text = atapi.ATFieldProperty('text')  
    webaddress = atapi.ATFieldProperty('webaddress')
    resourcelanguage = atapi.ATFieldProperty('resourcelanguage')    
    country = atapi.ATFieldProperty('country')
    region = atapi.ATFieldProperty('region')
    economicbloc = atapi.ATFieldProperty('economicbloc')                                   
    resourcetype = atapi.ATFieldProperty('resourcetype')   
    resourcesubject = atapi.ATFieldProperty('resourcesubject')
    
    # security settings
    security = ClassSecurityInfo()

    security.declarePublic('getSlaveVocab')
    def getRegionVocab(self, master):
        return REGIONS[master]        

    def getEconomicblocVocab(self, master):
        return ECONOMICBLOCS[master]       
    

atapi.registerType(Resource, PROJECTNAME)
